﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.RFQ.Controls
{
    public partial class ModalPublicRFQControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblRFQToken.Text = RSCommonFunctions.GetRandomToken();
            }
        }
        public void ShowModal(int rfqid)
        {
            ResetFields();
            RSDataLookupLoader.LoadPublicTypeDDL("INDUSTRY_CAT", ddlIndustryType);
            if (rfqid > 0)
            {
                LoadPublicRFQData(rfqid);
                hfRFQID.Value = rfqid.ToString();
                btnSave.Visible = false;
                pnlToken.Visible = false;
            }
        
            mpeModalRFQControl.Show();
        }
        protected void btnModalOK_Click(Object sender, EventArgs e)
        {
            mpeModalRFQControl.Hide();
            hfRFQID.Value = "";
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //RSUserIdentity userIdentity = GetRSUserIdentity();
            //if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            //{
            //    int companyid = userIdentity.RSCompanyId;
            //    int RFQid = hfRFQID.Value == "" ? 0 : Convert.ToInt32(hfRFQID.Value);
            //    if (companyid > 0 && RFQid > 0)
            //    {
            //        SqlCommand sqlCmd = new SqlCommand();
            //        SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            //        SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQID", SqlDbType.Int, 0, ParameterDirection.Input, RFQid);
            //        SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
            //        SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblRFQ WHERE RFQID=@RFQID AND CompanyId=@companyid");
            //        SQLHelper.ExecuteScalarCmd(sqlCmd);

            //        int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            //        lblMessage.Text = "RFQ Deleted Successfully!";
            //        btnSave.Visible = false;
            //        btnDelete.Visible = false;
            //    }
            //}
        }


        public void LoadPublicRFQData(int rfqid)
        {
            hfRFQID.Value = rfqid.ToString();
            if (rfqid > 0)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity();
                String sql = "SELECT * FROM rs_tblRFQ WHERE RFQID=@RFQID";

                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@RFQID";
                param.Value = rfqid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtContactName.Text = myDataReader["ContactName"].ToString();
                    txtEmail.Text = myDataReader["ContactEmail"].ToString();
                    txtMobile.Text = myDataReader["ContactMobile"].ToString();
                    txtOffice.Text = myDataReader["ContactOffice"].ToString();
                    txtSubject.Text = myDataReader["Subject"].ToString();
                    txtAdditionalInstruction.Text = myDataReader["AdditionalInstruction"].ToString();
                    txtTerm.Text = myDataReader["Term"].ToString();
                    txtDescription.Text = myDataReader["Description"].ToString();
                    if (myDataReader["BidCloseDt"] != DBNull.Value)
                    {
                        DateTime bidclosedt = Convert.ToDateTime(myDataReader["BidCloseDt"].ToString());
                        txtCloseDate.Text = bidclosedt.ToShortDateString();
                    }
                    if (myDataReader["IndustryType"] != DBNull.Value)
                        ddlIndustryType.SelectedValue = myDataReader["IndustryType"].ToString();

                }
                myConnection.Close();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string rfqtoken1 = lblRFQToken.Text;
            string rfqtoken2 = txtRFQToken.Text;
            if (rfqtoken1 == rfqtoken2)
            {
                int RFQID = hfRFQID.Value == "" ? 0 : int.Parse(hfRFQID.Value);
                int IndustryType = int.Parse(ddlIndustryType.SelectedValue);
                string CloseDate = txtCloseDate.Text;
                string ContactName = txtContactName.Text;
                string ContactMobile = txtMobile.Text;
                string ContactOffice = txtOffice.Text;
                string ContactEmail = txtEmail.Text;
                string Subject = txtSubject.Text;
                string AdditionalInstruction = txtAdditionalInstruction.Text;
                string Term = txtTerm.Text;
                string Description = txtDescription.Text;

                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@IndustryType", SqlDbType.Int, 0, ParameterDirection.Input, IndustryType);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactName", SqlDbType.NVarChar, 50, ParameterDirection.Input, ContactName);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@BidCloseDt", SqlDbType.DateTime, 0, ParameterDirection.Input, Convert.ToDateTime(CloseDate));
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactMobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, ContactMobile);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactOffice", SqlDbType.NVarChar, 15, ParameterDirection.Input, ContactOffice);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, ContactEmail);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Subject", SqlDbType.NVarChar, 250, ParameterDirection.Input, Subject);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@AdditionalInstruction", SqlDbType.NVarChar, -1, ParameterDirection.Input, AdditionalInstruction);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Term", SqlDbType.NVarChar, -1, ParameterDirection.Input, Term);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, -1, ParameterDirection.Input, Description);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQToken", SqlDbType.NVarChar, 50, ParameterDirection.Input, rfqtoken1);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQID", SqlDbType.Int, 0, ParameterDirection.Input, RFQID);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSavePublicRFQ");
                SQLHelper.ExecuteScalarCmd_ReadyServiceMain(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                //Generate Email
                //if (txtEmail.Text.Length > 6)
                //{
                //    string supportemail = ConfigurationManager.AppSettings["ReadyServiceSupportEmail"].ToString();
                //    SendConfirmationEmail(txtEmail.Text, supportemail, returnValue.ToString(), rfqtoken1);
                    lblMessage.Text = "Thank you.  Confirmation has been sent to the email specified.<br/>Please follow the instruction in the email to confirm the RFQ.";
                //}
                btnSave.Visible = false;
            }
            else
            {
                lblMessage.Text = "The RFQ Token you entered does not match the RFQ Token displayed.";
                lblMessage.ForeColor = System.Drawing.Color.DarkRed;
            }
        }


        private void SendConfirmationEmail(string toemail, string fromemail, string rfqid, string rfqtoken)
        {
            string body = "ReadyService RFQ Confirmation:  <br/><br/>" +
                "Please click on the URL to complete your Request For Quote (RFQ) in ReadyService.<br>" +
                "URL: " + ConfigurationManager.AppSettings["ReadyServiceRFQConfirmURL"].ToString() + "?p1=" + rfqid + "&p2=" + rfqtoken;

            string subject = "ReadyService RFQ Confirmation";
            if (fromemail.Length > 0 && toemail.Length > 0)
                RSCommonFunctions.SendEmail(body, subject, fromemail, toemail);
        }
        private void ResetFields()
        {
            txtCloseDate.Text = "";
            txtContactName.Text = "";
            txtMobile.Text = "";
            txtOffice.Text = "";
            txtEmail.Text = "";
            ddlIndustryType.SelectedValue = "0";
            txtSubject.Text = "";
            txtAdditionalInstruction.Text = "";
            txtTerm.Text = "";
            txtDescription.Text = "";
            btnSave.Visible = true;
            pnlToken.Visible = true;
            lblMessage.ForeColor = System.Drawing.Color.Blue;
        }
    }
}